
import { Context } from 'koa';
import { validateDto } from '@/utils/validateDto';
import { CreateTableInfoDto, UpdateTableInfoDto, QueryTableInfoDto, DeleteTableInfoDto, SelectOneTableInfoDto, CreateCodeTableInfoDto } from '@/dto/tableInfo.dto';
import tableInfoService from '@/services/tableInfo.service';

class TableInfoController {
    async selectList(ctx: Context) {

        const reqData = ctx.request.body as QueryTableInfoDto;
        const errors = await validateDto(QueryTableInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await tableInfoService.selectList(reqData)
        ctx.body = {
            code: 200,
            message: "列表查询成功",
            data:resData
        };
    }
    async selectOne(ctx: Context) {
        const reqData = ctx.request.body as SelectOneTableInfoDto;
        const errors = await validateDto(SelectOneTableInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await tableInfoService.selectOne(reqData)

        
        ctx.body = {
            code: 200,
            message:"详情查询成功",
            data: resData,

        };
    }

    async add(ctx: Context) {
        const userId = ctx.state.userId as number;
        const reqData = ctx.request.body as CreateTableInfoDto;
        //reqData.userId = reqData.userId || userId; //暂时注释
        const errors = await validateDto(CreateTableInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await tableInfoService.add(reqData);   
        
        ctx.body = {
        code: 200,
        message:"添加成功",
        data:{id:resData.id},
        };

    }

    async update(ctx: Context) {
        const reqData = <UpdateTableInfoDto>ctx.request.body
        const errors = await validateDto(UpdateTableInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await tableInfoService.update(reqData);   


        ctx.body = {
            code: 200,
            message:"更新成功",
            data: {},
        };

    }
    async delete(ctx: Context) {
        const reqData = ctx.request.body as DeleteTableInfoDto;
        const errors = await validateDto(DeleteTableInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await tableInfoService.delete(reqData);

        ctx.body = {
            code: 200,
            message:"删除成功",
            data: {},
        };

    }

    async createCode(ctx: Context) {
        const reqData = ctx.request.body as CreateCodeTableInfoDto;
        const errors = await validateDto(CreateCodeTableInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await tableInfoService.createCode(reqData)

        
        ctx.body = {
            code: 200,
            message:"详情查询成功",
            data: resData,

        };
    }


}

export default new TableInfoController();   
    
    